<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="种类" prop="category">
        <el-input v-model="dataForm.category" placeholder="种类"></el-input>
      </el-form-item>
      <el-form-item label="规格等级" prop="specifications">
        <el-input
          v-model="dataForm.specifications"
          placeholder="规格等级"
        ></el-input>
      </el-form-item>
      <el-form-item label="计量单位" prop="unit">
        <el-input v-model="dataForm.unit" placeholder="计量单位"></el-input>
      </el-form-item>
      <el-form-item label="零售超市价格" prop="supermarketPrice">
        <el-input
          v-model="dataForm.supermarketPrice"
          placeholder="零售超市价格"
        ></el-input>
      </el-form-item>
      <el-form-item label="零售集市价格" prop="marketPrice">
        <el-input
          v-model="dataForm.marketPrice"
          placeholder="零售集市价格"
        ></el-input>
      </el-form-item>
      <el-form-item label="时间" prop="date">
        <el-input v-model="dataForm.date" placeholder="时间"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: "",
        category: "",
        specifications: "",
        unit: "",
        supermarketPri: "",
        marketPrice: "",
        date: ""
      },
      dataRule: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        category: [
          { required: true, message: "类型不能为空", trigger: "blur" }
        ],
        specifications: [
          { required: true, message: "规格等级不能为空", trigger: "blur" }
        ],
        unit: [
          { required: true, message: "计量单位不能为空", trigger: "blur" }
        ],
        supermarketPrice: [
          { required: true, message: "零售超市价格不能为空", trigger: "blur" }
        ],
        marketPrice: [
          { required: true, message: "零售集市价格不能为空", trigger: "blur" }
        ],
        date: [{ required: true, message: "时间不能为空", trigger: "blur" }]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/huinong/consumerGoods/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.id = data.consumerGoods.id;
              this.dataForm.name = data.consumerGoods.name;
              this.dataForm.category = data.consumerGoods.category;
              this.dataForm.specifications = data.consumerGoods.specifications;
              this.dataForm.unit = data.consumerGoods.unit;
              this.dataForm.supermarketPrice = data.consumerGoods.supermarketPrice;
              this.dataForm.marketPrice = data.consumerGoods.marketPrice;
              this.dataForm.date = data.consumerGoods.date;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/huinong/consumerGoods/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              name: this.dataForm.name,
              category: this.dataForm.category,
              specifications: this.dataForm.specifications,
              unit: this.dataForm.unit,
              supermarketPrice: this.dataForm.supermarketPrice,
              marketPrice: this.dataForm.marketPrice,
              date: this.dataForm.date
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",

                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
